<template>
  <div class="login-container">
    <!-- 登录导航 -->
   <van-nav-bar
   class="page-nav-bar"
    title="登录"
    >
    <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <!-- 表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-cell-group>
        <!-- 手机号 -->
        <van-field
          v-model="user.mobile"
          left-icon="smile-o"
          placeholder="输入手机号"
          :rules="userFormRules.mobile"
          type="number"
          maxlength="11"
          name="mobile"
        >
      <!-- 左侧字体图标 -->
         <template #left-icon>
          <i class="toutiao toutiao-shouji"> </i>
        </template>
        </van-field>
        <!-- 验证码 -->
        <van-field
          v-model="user.codeSms"
          placeholder="输入验证码"
          :rules="userFormRules.code"
          type="number"
          maxlength="6"
        >
        <!-- 左侧字体图标 -->
        <template #left-icon>
        <i class="toutiao toutiao-yanzhengma"></i>
        </template>
        <!-- 右侧插入按钮 -->
        <template #button>
          <!-- 倒计时 -->
           <van-count-down :time="1000*60"  format=" ss s" v-if="isCountDownShow"
            @finish="isCountDownShow=false" />
          <!-- 按钮 -->
         <van-button
          v-else
          round
          size="small"
          type="default"
          class="send-sms-btn"
          @click="onSendSms()"
          native-type="button"
          >获取验证码</van-button>
         </template>
        </van-field>
    </van-cell-group>
          <div class="login-btn-wrap">
            <van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
          </div>
      </van-form>
  </div>
</template>

<script>
import { loginAPI, getSmsCodeAPI } from '../../api/index.js'
export default {
  name: 'loginIndex',
  data () {
    return {
      // 是否显示倒计时的变量
      isCountDownShow: false,
      // 登录框里面的val ,包括手机号,验证码
      user: {
        mobile: '13911111111',
        codeSms: '246810'
      },
      // input框里面的正则表达
      userFormRules: {
        mobile: [{ required: true, message: '手机号不能为空' },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: '手机格式错误'
          }],
        code: [{ required: true, message: '手机号不能为空' },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }]

      }
    }
  },
  methods: {
    // 点击获得验证码
    async onSendSms () {
      // 验证手机号
      try {
        // validate是vant的方法 配合ref可以使用部分表单验证规则
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.log('验证失败', err)
      }
      // 倒计时状态
      this.isCountDownShow = true
      // 发送验证码给手机
      try {
        const res = await getSmsCodeAPI(
          this.user.mobile
        )
        this.$toast('发送成功')
        console.log(res)
      } catch (err) {
        // 发送失败关闭倒计时
        this.isCountDownShow = false
        // 判断为短时间发送多次,告诉用户为什么失败
        if (err.response.status === 429) {
          this.$toast('发送太频繁了,请稍后再试')
        } else {
          this.$toast('发送失败,请稍后再试')
        }
        // console.log('发送失败', err)
      }
    },
    // 点击登录
    async onSubmit () {
      // 1.获取表单
      // const user = this.user

      // 2.表单验证

      // 在组件中必须通过this.$toast调用 Toast组件
      this.$toast.loading({
        message: '登陆中...',
        forbidClick: true, // 禁用背景点击
        duration: 0
      })
      // 3.提交表单请求登录
      try {
        const { data } = await loginAPI({
          mobile: this.user.mobile,
          code: this.user.codeSms
        })
        // console.log(res.data.data)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功')
        // 成功后跳转页面
        this.$router.back()
        // console.log('登录成功', data)
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机号或验证码错误')
          console.log('手机号或验证码错误', err)
        } else {
          this.$toast.fail('登录失败，稍后重试')
          console.log('登录失败，稍后重试', err)
        }
      }
    }

  }
}
</script>

<style lang="less" scoped>
  .login-container{
    .toutiao{
      font-size: 37px;
    }
    .send-sms-btn {
    // width: 152px;
    // line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  }
  .login-btn-wrap{
    padding: 53px 33px;
    .login-btn{
      background-color: #6db4fb;
      border: none;
    }
  }
</style>
